package it.softphone.cmc.server.service;

import it.softphone.cmc.server.service.exception.CMCException;
import it.softphone.cmc.shared.entity.Account;
import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.cmc.shared.entity.Tenant;
import it.softphone.cmc.shared.entity.TenantUser;
import it.softphone.cmc.shared.entity.Transaction;
import it.softphone.cmc.shared.entity.Version;
import it.softphone.commons.json.server.JSONException;

import java.util.List;


public interface SystemManagerService extends EntityManagerBaseService {
	
	/**
	 * 
	 * @param defaultVersion
	 * @return
	 * @throws CMCException
	 */
	Version getLastVersion(final String version ) throws CMCException;
	

	///////////////////////////////////////////////////////
	///
	/// ATTRIBUTES
	///
	///////////////////////////////////////////////////////
	Attributes makeAttributes();
	Attributes makeAttributesFromJson( it.softphone.commons.json.server.JSONObject object ) throws it.softphone.commons.json.server.JSONException;
	
	
	///////////////////////////////////////////////////////
	///
	/// TRANSACTION
	///
	///////////////////////////////////////////////////////
	Transaction makeTransactionFromJson( it.softphone.commons.json.server.JSONObject object ) throws it.softphone.commons.json.server.JSONException;
	Transaction makeTransaction( );
	
	/**
	 * 
	 * @param transaction
	 */

	void createTransaction( Transaction transaction )  throws CMCException;
	void storeTransaction( Transaction transaction) throws CMCException;


	///////////////////////////////////////////////////////
	///
	/// TENANT
	///
	///////////////////////////////////////////////////////
	Tenant makeTenantFromJson( it.softphone.commons.json.server.JSONObject object ) throws JSONException;
	Tenant makeTenant() ;
	
	/**
	 * 
	 * @param tenant
	 * @return true if tenant has been created. false if tenant is already present into archive
	 * @throws CMCException
	 */
	boolean createTenant( Tenant tenant ) throws CMCException;	
	void storeTenant( Tenant tenant ) throws CMCException;
	void deleteTenant( Tenant tenant ) throws CMCException;
	
	
	void findAllTenants( QueryTask<List<Tenant>> task ) throws CMCException;
	
	Tenant findTenantById( String nameOfId, String valueOfId ) throws CMCException;
	Tenant findSystemTenant() throws CMCException;

	
	///////////////////////////////////////////////////////
	///
	/// ACCOUNT
	///
	///////////////////////////////////////////////////////

	Account makeAccountFromJson( it.softphone.commons.json.server.JSONObject object ) throws it.softphone.commons.json.server.JSONException;
	Account makeAccount( );

	/**
	 * 
	 * @param id
	 * @return
	 * @throws CMCException
	 */
	Account findAccountById( final String id) throws CMCException;
	
	/**
	 * 
	 * @param email
	 * @param password
	 * @param deviceId (optional)
	 * @param requestUrl
	 * @return requestToken - temporary uid (time based expiration)
	 * @throws CMCException
	 */
	String requestAccountCreation( String email, String password, String deviceId, String requestUrl ) throws CMCException;
	
	/**
	 * 
	 * @param account
	 * @param user
	 * @param validateRegistration
	 * @return
	 * @throws CMCException
	 */
	Account confirmAccountCreation( String requestToken  ) throws CMCException;

	/**
	 * 
	 * @param account
	 * @param password
	 * @param activeRegistration
	 * @return
	 * @throws CMCException
	 */
	public Account createAccount( final Account account, final String password, final boolean activeRegistration ) throws CMCException;

	/**
	 * 
	 * @param account
	 * @throws CMCException
	 */
	public void storeAccount( final Account account ) throws CMCException;
	

	/**
	 * factory method
	 * @return
	 */
	Account.Preference makeAccountPreference();
	
	/**
	 * 
	 * @param account
	 * @param task
	 * @throws CMCException
	 */
	void findAccountPreferences( Account account, QueryTask<List<Account.Preference>> task ) throws CMCException; 
	
	void createAccountPreference( Account account, Account.Preference preference ) throws CMCException;
	
	void deleteAccountPreferenceByTenant( Account Account, Tenant tenant ) throws CMCException;
	
	///////////////////////////////////////////////////////
	///
	/// TENANT USER
	///
	///////////////////////////////////////////////////////

	TenantUser makeUserFromJson( it.softphone.commons.json.server.JSONObject object ) throws it.softphone.commons.json.server.JSONException;
	TenantUser makeUser( );

	
	/**
	 * 
	 * @param user
	 * @param account
	 * @return
	 * @throws CMCException
	 */
	TenantUser createUser( TenantUser user, Account account, boolean enable ) throws CMCException;
	
	/**
	 * 
	 * @param user
	 * @throws CMCException
	 */
	void storeUser( TenantUser user, boolean enable )  throws CMCException;

	/**
	 * 
	 * @param account
	 * @return
	 * @throws CMCException
	 */
	TenantUser findUserByAccount( final Account account ) throws CMCException;
	
	/**
	 * 
	 * @param account
	 * @throws CMCException
	 */
	void activeRegistration( Account account ) throws CMCException;

	/**
	 * 
	 * @param account
	 * @return
	 */
	boolean isRegistrationActive( Account account );
	
	/**
	 * 
	 * @param registrationToken
	 * @return
	 */
	boolean isRequestTokenExpired( String registrationToken );
	
}
